package ch.reevolt.android.graphics.widget;

import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;

import ch.reevolt.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;

/**
 * 
 * @author Romain Cherix
 * 
 */
public class ButtonImageView extends ImageView implements OnClickListener,
		OnTouchListener {

	protected Handler buttonHandler;
	protected int aplha;

	protected int alphaValueOnDisabled = 30;
	protected int alphaValueOnEnabled = 100;

	private Timer timer;
	private TimerTask timerTask;

	private boolean enable = true;
	private boolean visible = true;

	public enum Behavior {
		PUSH, SWITCH
	}

	private Behavior ButtonBehavior = Behavior.SWITCH;

	private OnClickedListener listener;

	/**
	 * @param context
	 */
	public ButtonImageView(Context context) {
		super(context);
	}

	/**
	 * @param context
	 * @param attrs
	 */
	public ButtonImageView(Context context, AttributeSet attrs) {
		super(context, attrs);

		try {
			TypedArray a = context.obtainStyledAttributes(attrs,
					R.styleable.ButtonImageView);

			setAlphaValueDisabled(a.getInt(
					R.styleable.ButtonImageView_alphaValueOnDisabled, 30));
			setAlphaValueEnabled(a.getInt(
					R.styleable.ButtonImageView_alphaValueOnEnabled, 100));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		initialize();
	}

	/**
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public ButtonImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		try {
			TypedArray a = context.obtainStyledAttributes(attrs,
					R.styleable.ButtonImageView);

			setAlphaValueDisabled(a.getInt(
					R.styleable.ButtonImageView_alphaValueOnDisabled, 30));
			setAlphaValueEnabled(a.getInt(
					R.styleable.ButtonImageView_alphaValueOnEnabled, 100));
		} catch (Exception e) {
			e.printStackTrace();
		}

		initialize();
	}

	/**
	 * initialize the button
	 */
	protected void initialize() {

		setAlpha(alphaValueOnEnabled);

		buttonHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				ButtonImageView.this.setAlpha((int) (msg.arg1 * 2.55));
			}
		};

		this.setOnClickListener(this);
		this.setOnTouchListener(this);

		timer = new Timer();
	}

	public void setBehavior(Behavior behavior) {
		this.ButtonBehavior = behavior;
	}

	@Override
	public void onClick(View v) {

		if (ButtonBehavior == Behavior.PUSH)
			if (enable) {
				timerTask = new TimerTask() {
					byte value = (byte) 0;

					@Override
					public void run() {
						Message msg = new Message();
						msg.arg1 = value++;
						buttonHandler.sendMessage(msg);
						if (value == alphaValueOnEnabled)
							this.cancel();
					}
				};
				timer.scheduleAtFixedRate(timerTask, 0, 2);
			}
		if (listener != null)
			listener.onButtonClick(v);
	}

	/**
	 * 
	 * @param enable
	 *            the state of the button
	 */
	public void setEnable(boolean enable) {
		if (!enable) {
			this.setAlpha(alphaValueOnDisabled);
			this.enable = false;
		} else {
			this.setAlpha((int) (alphaValueOnEnabled * 2.55));
			this.enable = true;
		}
	}

	public boolean isEnable() {
		return this.enable;
	}

	public void setAlphaValueDisabled(int value) {
		alphaValueOnDisabled = value;
	}

	public void setAlphaValueEnabled(int value) {
		alphaValueOnEnabled = value;
	}

	/**
	 * 
	 * @return true if the button is visible
	 */
	public boolean isVisible() {
		return visible;
	}

	/**
	 * 
	 * @param visible
	 *            the visibility of the button
	 */
	public void setVisible(boolean visible) {
		if (!visible) {
			this.setAlpha(alphaValueOnDisabled);
			this.visible = false;
		} else {
			this.setAlpha((int) (alphaValueOnEnabled * 2.55));
			this.visible = true;
		}
	}

	/**
	 * Interface with listener
	 * 
	 * @param listener
	 */
	public void setOnClickedListener(OnClickedListener listener) {
		this.listener = listener;
	}

	public interface OnClickedListener {
		public void onButtonClick(View v);
	}

	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			actionOnPress();
			break;
		case MotionEvent.ACTION_UP:
			actionOnRelease();
			break;
		}

		return false;
	}

	public void actionOnPress() {

	}

	public void actionOnRelease() {

	}

}
